package net.wangds.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtils {

    private static Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);

    public static String suffix(String filename){
        if(StringUtils.isBlank(filename)){
            return "";
        }
        int sepIdx = filename.lastIndexOf('.');
        if(sepIdx>=0){
            return filename.substring(sepIdx);
        }
        return "";
    }

    public static String suffixNoDot(String filename){
        if(StringUtils.isBlank(filename)){
            return "";
        }
        int sepIdx = filename.lastIndexOf('.');
        if(sepIdx>=0){
            return filename.substring(sepIdx+1);
        }
        return "";
    }


    public static ByteBuffer readFile(File file){
        if(file==null||!file.exists()||!file.canRead()){
            return ByteBuffer.allocate(0);
        }

        try(FileInputStream fis = new FileInputStream(file); FileChannel ch = fis.getChannel()){

            ByteBuffer bf = ByteBuffer.allocate(fis.available());
            ch.read(bf);
            return bf;
        } catch (IOException e) {
            LOGGER.error(e.getLocalizedMessage(), e);
        }

        return ByteBuffer.allocate(0);
    }

    public static void writeFile(File file, byte[] bytes, boolean append){

        if(file==null){
            return;
        }

        if(bytes==null){
            bytes = new byte[0];
        }

        if(!file.exists()){
            File p = file.getParentFile();
            if((!p.exists())&&!p.mkdirs()){
                LOGGER.error(String.format("无法创建要写入的文件\"%s\"", file.getPath()));
                return;
            }
        }

        try(FileOutputStream fos = new FileOutputStream(file, append);
            FileChannel ch = fos.getChannel()
        ) {
            ByteBuffer buf = ByteBuffer.wrap(bytes);
            ch.write(buf);
        } catch (IOException e) {
            LOGGER.error(e.getLocalizedMessage(), e);
        }


    }


    public static void writeFileSub(File file, byte[] bytes, long pos){

        if(file==null){
            return;
        }

        if(bytes==null){
            bytes = new byte[0];
        }

        if(!file.exists()){
            File p = file.getParentFile();
            if((!p.exists())&&!p.mkdirs()){
                LOGGER.error(String.format("无法创建要写入的文件\"%s\"", file.getPath()));
                return;
            }
        }

        try(FileOutputStream fos = new FileOutputStream(file, true);
            FileChannel ch = fos.getChannel()
        ) {
            ByteBuffer buf = ByteBuffer.wrap(bytes);
            ch.position(pos);
            ch.write(buf);
        } catch (IOException e) {
            LOGGER.error(e.getLocalizedMessage(), e);
        }
    }

}
